package tool.soft

import java.io.File
import dao._
import models.Tables
import org.joda.time.DateTime
import play.api.mvc.{AnyContent, Request}
import play.twirl.api.Html
import shared.Pojo._
import tool.Pojo._
import tool.{FormTool, Tool}
import tool.plot._
import tool.soft.AllSoft.SoftBT
import implicits.Implicits._
import models.Tables._
import play.api.data.FormBinding
import play.api.i18n.Messages
import utils.Utils
import upickle.default._

/**
 * Created by yz on 15/9/2020
 */
object AnnoSoft extends AnnoSoftT with SoftBT {

  override def getResultFileName: String = {
    "AllMet_with_Anno.csv"
  }

  override def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.mission.annoResult(missionId)
  }

  override def getFileInfos(workspaceDir: File, index: Int)(implicit matrixFileDataDao: MatrixFileDataDao, messages: Messages): List[FileInfoData] = {
    List(
      FileInfoData("AllMet_with_Anno.csv", messages("allMet")),
      FileInfoData("Class_Count.csv", messages("classCount")),
      FileInfoData("Class_Count_Pieplot.pdf", messages("piePlotByClassCounts")),
      FileInfoData("Class_Table.csv", messages("classTable")),
      FileInfoData("Class_Table_Stat.csv", messages("classTableStat")),
      FileInfoData("Class_Table_Stat_Pieplot.pdf", messages("piePlotByClassTotalConcentration")),
    )
  }

  override def produceConfigFile(workspaceDir: File, index: Int)(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    super.produceConfigFile(workspaceDir, index)
    data.lines().toTxtFile(Tool.getCalculateConfigTxtFile(workspaceDir))
    Utils.t2ConfigFile(workspaceDir, data)
  }

  override def producePlotConfigFile(workspaceDir: File, index: Int): Any = {
    Tool.producePlotConfigFile(AllPlotData(), ClassCountPie, workspaceDir)
    Tool.producePlotConfigFile(AllPlotData(), StatPie, workspaceDir)
  }

  def getArg(implicit request: Request[AnyContent], messages: Messages, formBinding: FormBinding): ujson.Value = {
    ujson.Obj()
  }

  override def produceInput(workspaceDir: File, resultDir: File)(implicit dao: MyDao): Unit = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceKeggInfoFile(rawDataFile, workspaceDir)
  }

  override def getCommandData(workspaceDir: File, resultDir: File, index: Int): List[CommandData] = {
    val command =
      s"""
         |${Tool.rScript} ${new File(Tool.rPath, "anno.R").unixPath}
         |${Tool.rScript} ${new File(Tool.rPath, "produce_meta_color.R").unixPath}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "class_sum_no_group.R").unixPath}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "class_sum_no_group_stat.R").unixPath}
         |${"Rscript".condaPath()} ${new File(Tool.rPath, "metabolite_class_pie_plot_no_group.R").unixPath} --pc ${StatPie.configFileName}
         |${Tool.rScript} ${new File(Tool.rPath, "super_class_count.R").unixPath}
         |${Tool.rScript} ${new File(Tool.rPath, "super_class_count_pie_plot.R").unixPath} --pc ${ClassCountPie.configFileName}
         |convert  -density 300 Class_Count_Pieplot.pdf  Class_Count_Pieplot.png
         |convert  -density 300 Class_Table_Stat_Pieplot.pdf  Class_Table_Stat_Pieplot.png
           """.stripMargin
    List(CommandData(workspaceDir, List(command)))
  }

  override def getHtml(missionName: String, missionId: Option[Int])(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.soft.anno(missionName, missionId)
  }

  override def getMission(implicit request: Request[AnyContent], matrixFileDataDao: MatrixFileDataDao, messages: Messages, formBinding: FormBinding): Tables.MissionRow = {
    val argJson = getArg
    val initInputs = List(data.file)
    val hasLibInputs = initInputs
    MissionRow(id = 0, missionName = s"${data.missionName}", kind = nameStr, userId = userId,
      startTime = new DateTime(), state = "preparing", arg = getArg, input = hasLibInputs)
  }

  override def produceInputFile(workspaceDir: File)(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceRawDataFile(userId, data.file, rawDataFile)
  }

  override def data(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    FormTool.annoForm.bindFromRequest().get
  }


}

